package com.greyu.ysj.controller;


import java.io.BufferedReader;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.checkerframework.checker.units.qual.h;

import com.alibaba.fastjson.JSONObject;


public class Test {

    private static int nowPhone;

    public static final int phone = 100000000;
    private static final int end = 100000040;
    private static final String  url = "http://192.168.16.110";
//    private static final String  url = "http://ec2-54-255-221-17.ap-southeast-1.compute.amazonaws.com";

    public static void main(String[] args) {
        nowPhone = phone;
        register("");  //绗竴娆℃敞鍐�

        create(1); //鍒涘缓
        setMoney();//鎶曡祫
        
    }

    //鍒涘缓
    private static void create(int count) {
        String code = loginInvited(); //鐧诲綍涓�涓�
        for (int i = 1; i < 4; i++) {   //娉ㄥ唽涓変釜
            nowPhone++;
            register(code);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (count < 3) {
                create(count + 1);  //姣忔敞鍐岀殑涓�涓� 鍐嶆鍘荤櫥褰�
            }
        }
    }

    //鎶曡祫
    private static void setMoney() {
        for (int i = phone; i < end; i++) {
            try {
                String token = loginToken(i + "");
                String[] split = token.split("%");
                setMoney(split[1], split[0]);
                setInvite(split[1], split[0]);
                Thread.sleep(100);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    //鐧婚檰
    private static String loginInvited() {
        Map<String, Object> map = new HashMap<>();
        map.put("phone", "15" + nowPhone + "");
        map.put("passWord", "a123456789");
        map.put("area", 86);
        String json = post(url+"/akbm_dfc/cbt/loginByPhone", map, "");
        try {
            
            JSONObject jsonObject =JSONObject.parseObject(json);
            JSONObject data = jsonObject.getJSONObject("data");
            String avatar = data.getString("avatar");
            System.out.println("鐧婚檰鎴愬姛锛�15" + nowPhone);
            return avatar;
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

    private static String loginToken(String phone) {
        Map<String, Object> map = new HashMap<>();
        map.put("phone", "15" + phone + "");
        map.put("passWord", "a123456789");
        map.put("area", 86);
        String json = post(url+"/akbm_dfc/cbt/loginByPhone", map, "");
        try {
        	JSONObject jsonObject =JSONObject.parseObject(json);
            JSONObject data = jsonObject.getJSONObject("data");
            String userId = data.getString("userId");
            String token = data.getString("token");
            System.out.println("鐧婚檰鎴愬姛" + userId);
            return userId + "%" + token;
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

    private static void register(String invitationCode) {
        Map<String, Object> map = new HashMap<>();
        map.put("phone", "15" + nowPhone + "");
        map.put("passWord", "a123456789");
        map.put("code", "123456");
        map.put("area", "86");
        map.put("avatar", invitationCode == null ? "" : invitationCode);
        String json = post(url+"/akbm_dfc/cbt/addUserByPhone", map, "");
        System.out.println(json);
    }

    private static void setMoney(String token, String userId) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("asset", "100000");
        map.put("coinId", 10000011);
        String json = post(url+"/akbm_dfc/cbt/setAsset", map, userId + "_" + token);
        System.out.println("AKB"+json);
        
        Map<String, Object> map1 = new HashMap<>();
        map1.put("userId", userId);
        map1.put("asset", "100000");
        map1.put("coinId", 10000016);
        String json1 = post(url+"/akbm_dfc/cbt/setAsset", map1, userId + "_" + token);
        System.out.println("PC"+json1);
        
        Map<String, Object> map2 = new HashMap<>();
        map2.put("userId", userId);
        map2.put("asset", "100000");
        map2.put("coinId", 10000017);
        String json2 = post(url+"/akbm_dfc/cbt/setAsset", map2, userId + "_" + token);
        System.out.println("USDT"+json2);
    }


    private static void setInvite(String token, String userId) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("coinId", "10000011");
        map.put("value", "50000");
        map.put("day", "7");
        String json = post(url+"/akbm_dfc/cbt/doInvest", map, userId + "_" + token);
        System.out.println(json);
    }

    public static String post(String url, Map<String, Object> param, String token) {
        StringBuilder builder = new StringBuilder();
        try {
            StringBuilder params = new StringBuilder();
            for (Map.Entry<String, Object> entry : param.entrySet()) {
                params.append(entry.getKey());
                params.append("=");
                params.append(entry.getValue().toString());
                params.append("&");
            }
            if (params.length() > 0) {
                params.deleteCharAt(params.lastIndexOf("&"));
            }
            URL restServiceURL = new URL(url + (params.length() > 0 ? "?" + params.toString() : ""));
            HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL.openConnection();
            httpConnection.setRequestMethod("POST");
            httpConnection.setRequestProperty("Accept", "application/json");
            httpConnection.setRequestProperty("authorization", token);
            // 璁剧疆鏄惁浠巋ttpUrlConnection璇诲叆锛岄粯璁ゆ儏鍐典笅鏄痶rue;
            httpConnection.setDoInput(true);
            // Post 璇锋眰涓嶈兘浣跨敤缂撳瓨
            httpConnection.setUseCaches(false);
            if (httpConnection.getResponseCode() != 200) {
                throw new RuntimeException("HTTP POST Request Failed with Error code : "
                        + httpConnection.getResponseCode());
            }
            InputStream inStrm = httpConnection.getInputStream();
            byte[] b = new byte[1024];
            int length;
            while ((length = inStrm.read(b)) != -1) {
                builder.append(new String(b, 0, length));
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return builder.toString();
    }
}


